package com.fanco.learn_datastruct_leetcode.juc;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * @author qiaowang 2021/8/23
 * 描述
 */
public class LockWaitNotifyDemo {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Thread thread = new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"在等待锁");
                condition.await();
                System.out.println(Thread.currentThread().getName()+"被唤醒，开始执行");
            }catch (Exception e) {
            } finally {
                lock.unlock();
            }
        }, "Thread A");
        thread.start();
        Thread thread1 = new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"获取到锁");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"准备释放锁");
                System.out.println(thread.getName() +"当前状态为" +thread.getState());
                condition.signalAll();
            } finally {
                lock.unlock();
            }
        }, "Thead B");
        thread1.start();
    }
}
